package com.wanmait.recycle.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.mapper.DataMapper;
import com.wanmait.recycle.mapper.DisposeMapper;
import com.wanmait.recycle.mapper.RubbishMapper;
import com.wanmait.recycle.pojo.Data;
import com.wanmait.recycle.pojo.Dispose;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.service.ConfigService;
import com.wanmait.recycle.service.DataService;
import com.wanmait.recycle.util.CalculateDataUtil;
import com.wanmait.recycle.util.PageContext;
import com.wanmait.recycle.util.Pager;

@Service
@Transactional
public class DataServiceImpl implements DataService {

	@Resource
	private DataMapper dataMapper;
	@Resource
	private RubbishMapper rubbishMapper;
	@Resource
	private DisposeMapper disposeMapper;
	@Resource
	private ConfigService configService;
		
	
	
	@Override
	public void insert(Data object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Data object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		dataMapper.deleteById(id);
	}

	@Override
	public void delete(String ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Data findById(Integer id) {
		// TODO Auto-generated method stub
		return dataMapper.findById(id);
	}
	@Override
	public PageInfo<Data> findAllByRubbishId(Integer id) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台数据分页").get("mDataPage")));
		List<Data> articles = dataMapper.findAllByRubbishId(id);
		PageInfo<Data> pageInfo = new PageInfo<Data>(articles,7);
		return pageInfo;
	}

	@Override
	public int getDataCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Data> findAll(Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Data> findAll(Data object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDataCount(Data object) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Data> findAll(Pager pager, Data object) {
		// TODO Auto-generated method stub
		return null;
	}
	@Cacheable(value="recycle",key="'findAllDatas'")
	@Override
	public List<Data> findAll() {
		// TODO Auto-generated method stub
		return dataMapper.findAll(1);
	}

	@Override
	public PageInfo<Data> findAllData(Integer enable) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台数据分页").get("mDataPage")));
		List<Data> datas = dataMapper.findAll(1);
		PageInfo<Data> pageInfo = new PageInfo<Data>(datas,7);
		return pageInfo;
	}

	/*启用和禁用 改变enable*/
	@Override
	public void changeEnable(Data data) {
		dataMapper.changeEnable(data);
		
	}

	@Override
	public PageInfo<Data> findAllByCriteria(Integer enable,String beginTime, String endTime, String searchStr) {
		if("".equals(beginTime)) {
			beginTime = null;
		}
		if("".equals(endTime)) {
			endTime = null;
		}
		if("".equals(searchStr)) {
			searchStr = null;
		}
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台数据分页").get("mDataPage")));
		List<Data> datas = dataMapper.findAllByCriteria(enable,beginTime, endTime, searchStr);
		PageInfo<Data> pageInfo = new PageInfo<Data>(datas,7);
		return pageInfo;
	}

	@Override
	public void addData(Data data) {
		// TODO Auto-generated method stub
	
		dataMapper.addData(data);
		
	}

	@Override
	public PageInfo<Data> findAllById(Integer id) {
		PageHelper.startPage(PageContext.getPageNum(), 6);
		List<Data> datas = dataMapper.findAllById(id);
		PageInfo<Data> pageInfo = new PageInfo<Data>(datas,7);
		return pageInfo;
	}

	@Override
	public PageInfo<Data> seachByTime(String beginTime, String endTime,Integer userinfoId) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("前台分页").get("webPage")));
		List<Data> datas = dataMapper.seachByTime(beginTime,endTime,userinfoId);
		PageInfo<Data> pageInfo = new PageInfo<Data>(datas,7);
		return pageInfo;
	}

	@Override
	public void saveData(Data data, String startTime, String stopTime) {
		dataMapper.saveData(data,startTime,stopTime);
		
	}
	
	@Override
	public Map<String, Double> dataStatistics(Integer nRubbishId, Integer nCityId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		
		Rubbish rubbish=rubbishMapper.selectByPrimaryKey(nRubbishId);
		double disposeCount=0;
		double dischargeCount=0;
		List<Data> datasDischarge=null;
		List<Data> datasDispose=null;
		if(nCityId==-1) {
			 datasDischarge=dataMapper.getNationalDatasConcern(rubbish,false);
			
			 datasDispose=dataMapper.getNationalDatasConcern(rubbish,true);
		}
		else {
			 datasDischarge=dataMapper.getDatasConcern(rubbish,nCityId,false);
			 
			 datasDispose=dataMapper.getDatasConcern(rubbish,nCityId,true);
		}
		disposeCount=CalculateDataUtil.getCount(beginTime, endTime, datasDispose);
		dischargeCount=CalculateDataUtil.getCount(beginTime, endTime, datasDischarge);
	
		Map<String, Double> map=new HashMap<>();
		map.put("disposeCount",disposeCount);
		map.put("dischargeCount",dischargeCount);
		return map;
	}

	@Override
	public List<Double> getDisChargeByYear(Date date) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Dispose> findTwoList() {
		// TODO Auto-generated method stub
		return disposeMapper.findTwoList();
	}

	@Override
	public void changeData(Data data, String startTime, String stopTime) {
		// TODO Auto-generated method stub
		dataMapper.changeData(data,startTime,stopTime);
	}
}
